/**
 *  Uses a hash-coded data storage method to store items
 *  from a file. a linked list is used as the method for
 *  dealing with collisions
 */

import java.util.*;
import java.io.*;

public class Hashing
{
	private HashTable table;
	private Scanner input;

	final int TABLESIZE = 600;

	public Hashing()
	{
		table = new HashTable(599);
		input = new Scanner(System.in);
	}

	public void search()
	{
		int idToFind;
		Item location;
		input = new Scanner(System.in);
		System.out.println("Testing search algorithm\n");
		System.out.print("Enter Id value to search for (-1 to quit) --> ");
		idToFind = input.nextInt();
		input.nextLine();

		while (idToFind >= 0)
		{
			location = (Item)(table.find(new Item(idToFind, 0)));
			if (location == null)
			System.out.println("Id = " + idToFind + "  No such part in stock");
			else
			System.out.println("Id = " + location.getId() +
				   "  Inv = " + location.getInv());
			System.out.println();
			System.out.print("Enter Id value to search for (-1 to quit) --> ");
			idToFind = input.nextInt();
			input.nextLine();
		}
	}

	public void stats()
	{
		System.out.println();
		System.out.println("Number of items = " + table.getSize());
		System.out.println("Number of buckets = " + table.getCapacity());

	}

	public void loadFile()
	{
		try{
			input = new Scanner(new File("file400.txt"));
		}
		catch(Exception e)
		{
		}

		int howMany = input.nextInt();
		input.nextLine();
		for (int k = 1; k <= howMany; k++)
		{
			int id = input.nextInt();
			int num = input.nextInt();
			input.nextLine();
			table.add(new Item(id, num));
		}
	}

	public static void main(String[] args)
	{
		Hashing test = new Hashing();

		test.loadFile();
		test.search();
		test.stats();
	}
}